<?php
/**
 * Cette classe est utilisée pour gérer la configuration des profils.
 */
class JckBrowserProfil
{
  // Visualisation des fichiers
  protected $browse               = false;
  protected $browse_auth          = true;
  protected $browse_credentials   = array();

  // Envois de fichiers
  protected $upload               = false;
  protected $upload_auth          = true;
  protected $upload_credentials   = array();

  // Envois de fichiers
  protected $delete               = false;
  protected $delete_auth          = true;
  protected $delete_credentials   = array();

  // Adresse
  protected $security_forward_url   = 'default/secure';
  protected $security_forward_code  = 403;

  // Nom du profil
  protected $name;

  // Chemin du dossier racine
  protected $path;
  protected $webPath;
  protected $doctype = array();


 


  //
  function __construct( $profilName )
  {
    sfApplicationConfiguration::getActive()->loadHelpers('I18N');
    try {
      if( $profilName == null || trim($profilName) === '' ) {
        throw new Exception(__("Profil is require",null, 'jckckeditor'));
      }
      $this->name = $profilName;

      // Récupération des données (globales)
      $profils = sfConfig::get('app_jckbrowser_profils');

      if( !isset($profils[$this->name]) ){
        throw new Exception(sprintf(__("Profil '%s' not found",null, 'jckckeditor'), $profilName));
      }

      $profil_dt = $profils[$this->name];

      // Path
      if( !isset($profil_dt['path']) ){
        throw new Exception(sprintf(__("Variable 'path' is required",null, 'jckckeditor'), $profilName));
      }
      $this->webPath = $profil_dt['path'];
      $this->path = new FileSystemEntry(sfConfig::get('sf_web_dir').$this->webPath);


      $sf_user = sfContext::getInstance()->getUser();

      // Browse
      if( isset($profil_dt['browse']) ){
        $this->browse = ($profil_dt['browse'] === true);
        // Requière un utilisateur authentifié
        if( isset($profil_dt['browse']['require_authenticated'])) {
          if( $profil_dt['browse']['require_authenticated'] === true ){
            $this->browse = $sf_user->isAuthenticated();
          } else {
            $this->browse = true;
          }
        }

        // Requière des droits spécifiques
        if( isset($profil_dt['browse']['credentials'])) {
          
          if( is_array($profil_dt['browse']['credentials']) ){
            $this->browse = $sf_user->hasCredential($profil_dt['browse']['credentials']);
          } else {
            $this->browse = false;
          }
        }
      }

      // Upload
      if( isset($profil_dt['upload']) ){
        $this->upload = ($profil_dt['upload'] === true);
        // Requière un utilisateur authentifié
        if( isset($profil_dt['upload']['require_authenticated'])) {
          if( $profil_dt['upload']['require_authenticated'] === true ){
            $this->upload = $sf_user->isAuthenticated();
          } else {
            $this->upload = true;
          }
        }

        // Requière des droits spécifiques
        if( isset($profil_dt['upload']['credentials'])) {

          if( is_array($profil_dt['upload']['credentials']) ){
            $this->upload = $sf_user->hasCredential($profil_dt['upload']['credentials']);
          } else {
            $this->upload = false;
          }
        }
      }

      // Delete
      if( isset($profil_dt['delete']) ){
        $this->delete = ($profil_dt['delete'] === true);
        // Requière un utilisateur authentifié
        if( isset($profil_dt['delete']['require_authenticated'])) {
          if( $profil_dt['delete']['require_authenticated'] === true ){
            $this->delete = $sf_user->isAuthenticated();
          } else {
            $this->delete = true;
          }
        }

        // Requière des droits spécifiques
        if( isset($profil_dt['delete']['credentials'])) {

          if( is_array($profil_dt['delete']['credentials']) ){
            $this->delete = $sf_user->hasCredential($profil_dt['delete']['credentials']);
          } else {
            $this->delete = false;
          }
        }
      }


      // Type de fichier générales
      $doctype = sfConfig::get('sf_jckbrowser_doctype');

      // Type de fichier du profil
      $profil_doctype = $profil_dt['filetype'];
      foreach( $profil_doctype as $type=>$mimes ){
        foreach( $mimes as $mime ){
          $this->doctype[$mime] = $type;
        }
      }



    } catch (Exception $e) {
      throw new Exception($e->getMessage());
    }
  }

  public function allowMimeType( $mimeType ){
    if( isset($this->doctype[$mimeType]) ){
      return $this->doctype[$mimeType];
    }
    return false;
  }

  public function allowBrowse()
  {
    return $this->browse;
  }

  public function allowDelete()
  {
    return $this->delete;
  }

  public function allowUpload()
  {
    return $this->upload;
  }

  public function getPath()
  {
    return $this->path;
  }

  public function getWebPath()
  {
    return $this->webPath;
  }

  public function getName()
  {
    return $this->name;
  }
}